//
//  JLRequestable.swift
//  JLApiCenter
//
//  Created by Jiang Chencheng on 2024/7/31.
//

import Foundation
import Alamofire

public protocol JLBaseRequestable {
    var baseURL: URL { get }
    var method: HTTPMethod { get }
    var path: String { get }
    var parameters: [String: String] { get }
}

public protocol JLRequestable: JLBaseRequestable, URLRequestConvertible {}


extension JLRequestable {
    
    public var baseURL: URL {
        // TODO: - 填入项目的 baseURL
        return URL(string: "https://httpbin.org")!
    }
    
    public func asURLRequest() throws -> URLRequest {
        let url = baseURL.appendingPathComponent(path)
        var request = URLRequest(url: url)
        request.method = method
        switch method {
        case .get:
            request = try URLEncodedFormParameterEncoder().encode(parameters, into: request)
        case .post:
            request = try JSONParameterEncoder().encode(parameters, into: request)
        default: break
        }
        return request
    }
    
    
}

